//
//  RootViewController.m
//  CustomCellExample
//

#import "RootViewController.h"

@implementation RootViewController

- (void)switchAction:(id)sender {
	NSLog(@"switch changed");
}

- (void)buttonAction:(id)sender {
	NSLog(@"button tapped");
}

- (void)viewDidLoad {
    [super viewDidLoad];

	mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
	[mySwitch addTarget:self
				 action:@selector(switchAction:)
	   forControlEvents:UIControlEventValueChanged];
	[mySwitch setOn:YES];
	
	myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[myButton setFrame:CGRectMake(0,0,80,30)];
	[myButton addTarget:self
				 action:@selector(buttonAction:)
	   forControlEvents:UIControlEventTouchUpInside];
	[myButton setTitle:@"Tap Me!" forState:UIControlStateNormal];
	
	arrCells = [[NSArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arrCells count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	UITableViewCell *cell = nil;
	static NSString *CellIdentifier = @"Cell";
	static NSString *ViewCellIdentifier = @"ViewCell";
	
	switch (indexPath.row) {
		case 0:
			cell = [tableView dequeueReusableCellWithIdentifier:ViewCellIdentifier];
			if (cell == nil)
				cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault
											reuseIdentifier:ViewCellIdentifier] autorelease];
			
			cell.textLabel.text = [arrCells objectAtIndex:indexPath.row];
			((MyCustomCell *)cell).view = mySwitch;
			break;
			
		case 1:
			cell = [tableView dequeueReusableCellWithIdentifier:ViewCellIdentifier];
			if (cell == nil)
				cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault
											reuseIdentifier:ViewCellIdentifier] autorelease];
			
			cell.textLabel.text = [arrCells objectAtIndex:indexPath.row];
			((MyCustomCell *)cell).view = myButton;
			break;
			
		default:
			cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
			if (cell == nil)
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
											   reuseIdentifier:CellIdentifier] autorelease];
			
			cell.textLabel.text = [arrCells objectAtIndex:indexPath.row];
			break;
	}
	
	return cell;
}

- (void)dealloc {
	
	[arrCells release];
	[mySwitch release];
	[myButton release];
	
    [super dealloc];
}


@end

